package org.tuoren.official.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.tuoren.common.idempotent.annotation.RepeatSubmit;
import org.tuoren.common.log.annotation.Log;
import org.tuoren.common.web.core.BaseController;
import org.tuoren.common.mybatis.core.page.PageQuery;
import org.tuoren.common.core.domain.R;
import org.tuoren.common.core.validate.AddGroup;
import org.tuoren.common.core.validate.EditGroup;
import org.tuoren.common.log.enums.BusinessType;
import org.tuoren.common.excel.utils.ExcelUtil;
import org.tuoren.official.domain.vo.TFloatingAdsVo;
import org.tuoren.official.domain.bo.TFloatingAdsBo;
import org.tuoren.official.service.ITFloatingAdsService;
import org.tuoren.common.mybatis.core.page.TableDataInfo;

/**
 * 悬浮广告
 *
 * @author Lion Li
 * @date 2025-05-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/official/floatingAds")
public class TFloatingAdsController extends BaseController {

    private final ITFloatingAdsService tFloatingAdsService;

    /**
     * 前台-查询悬浮广告列表
     */
    @SaIgnore
    @GetMapping("/frontList")
    public TableDataInfo<TFloatingAdsVo> frontList(TFloatingAdsBo bo) {
        return tFloatingAdsService.frontList(bo);
    }
    /**
     * 查询悬浮广告列表
     */
    @SaCheckPermission("official:floatingAds:list")
    @GetMapping("/list")
    public TableDataInfo<TFloatingAdsVo> list(TFloatingAdsBo bo, PageQuery pageQuery) {
        return tFloatingAdsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出悬浮广告列表
     */
    @SaCheckPermission("official:floatingAds:export")
    @Log(title = "悬浮广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TFloatingAdsBo bo, HttpServletResponse response) {
        List<TFloatingAdsVo> list = tFloatingAdsService.queryList(bo);
        ExcelUtil.exportExcel(list, "悬浮广告", TFloatingAdsVo.class, response);
    }

    /**
     * 获取悬浮广告详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("official:floatingAds:query")
    @GetMapping("/{id}")
    public R<TFloatingAdsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tFloatingAdsService.queryById(id));
    }

    /**
     * 新增悬浮广告
     */
    @SaCheckPermission("official:floatingAds:add")
    @Log(title = "悬浮广告", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TFloatingAdsBo bo) {
        return toAjax(tFloatingAdsService.insertByBo(bo));
    }

    /**
     * 修改悬浮广告
     */
    @SaCheckPermission("official:floatingAds:edit")
    @Log(title = "悬浮广告", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TFloatingAdsBo bo) {
        return toAjax(tFloatingAdsService.updateByBo(bo));
    }

    /**
     * 删除悬浮广告
     *
     * @param ids 主键串
     */
    @SaCheckPermission("official:floatingAds:remove")
    @Log(title = "悬浮广告", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tFloatingAdsService.deleteWithValidByIds(List.of(ids), true));
    }
}
